﻿using System.Data.Entity;
using System.Web.Mvc;
using Autofac;
using Autofac.Integration.Mvc;
using RealtReview.DataAccess;
using RealtReview.DataAccess.Interfaces;
using RealtReview.DataAccess.Repositories;
using RealtyReview.Business.Interfaces;
using RealtyReview.Business.Services;

namespace RealtyReview
{
    public class AutofacConfig
    {
        public static void Register()
        {
            var builder = new ContainerBuilder();
            builder.RegisterControllers(typeof(MvcApplication).Assembly);
            RegisterServices(builder);
            RegisterRepositories(builder);
            builder.RegisterType<RealtReviewContext>().As<DbContext>().SingleInstance();
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }

        private static void RegisterServices(ContainerBuilder builder)
        {
            builder.RegisterType<ManagementService>().As<IManagementService>();
            builder.RegisterType<NewsService>().As<INewsService>();
            builder.RegisterType<ReviewsService>().As<IReviewsService>();
            builder.RegisterType<UserService>().As<IUserService>();
        }

        private static void RegisterRepositories(ContainerBuilder builder)
        {
            builder.RegisterType<AddressRepository>().As<IAddressRepository>();
            builder.RegisterType<ApartmentRepository>().As<IApartmentRepository>();
            builder.RegisterType<CommentsRepository>().As<ICommentsRepository>();
            builder.RegisterType<NewsRepository>().As<INewsRepository>();
            builder.RegisterType<ReviewRepository>().As<IReviewRepository>();            
            builder.RegisterType<UserMessageRepository>().As<IUserMessageRepository>();
        }
    }
}